<?php

class User {
	//Bool to check if object constructed and initialized okay.
	public $Initialized = false;
	
	//Unique random identifier for user to keep
	public $UID = false;
	
	public $FirstName = false;
	public $LastName = false;
	public $DisplayName = false;
	public $Email = false;
	public $IsActive = false;
	public $LastAccessedDate = false;
	public $CreationDate = false;
	
	//If true, when request to follow this user is made, follow is immediately made.
	public $RequireFollowApproval = false;
	
	public function __construct($userResult=false) {
		if ($userResult) {
			try {
				$this->UID = $userResult['uid'];
				$this->FirstName = $userResult['firstName'];
				$this->LastName = $userResult['lastName'];
				$this->DisplayName = $userResult['displayName'];
				/*$this->LastAccessedDate = strtotime($userResult['LastAccessedDate']);
				$this->CreationDate = strtotime($userResult['CreationDate']);
				$this->RequireFollowApproval = $userResult['RequireFollowApproval'];*/
				$this->Initialized = true;
			} catch(Exception $e) {
				$this->Initialized = false;
			}
		}
	}
	
	public static function Create($firstName, $lastName, $email, $password, $forgottenQuestion, $forgottenAnswer) {
		if ($firstName!="" && $lastName!="" && $email!="" && $password!="" && $forgottenQuestion!="" && $forgottenAnswer!="") {
			$password = str_hash($password, $email);
			$forgottenAnswer = str_hash($forgottenAnswer, $email);
			return new User(Sql::ExecuteScalar("sp_newUser", $email, $password, $firstName." ".$lastName, $firstName, $lastName, $forgottenQuestion, $forgottenAnswer));
		}
		return false;
	}
	
	public static function GetUserByUid($uid) {
		$user = new User(Sql::ExecuteScalar("sp_getUserById", $uid));
		if ($user && $user->Initialized) {
			return $user;
		}
		return false;
	}
	
	public static function GetUserByEmail($email) {
		$user = new User(Sql::ExecuteScalar("sp_getUserByEmail", $email));
		if ($user && $user->Initialized) {
			return $user;
		}
		return false;
	}
	
	public static function GetSecretQuestions() {
		return Sql::Execute("sp_getSecretQuestions");
	}
	
	public static function Authenticate($email, $password) {
		$password = str_hash($password, $email);
		return new User(Sql::ExecuteScalar("sp_login", $email, $password));
	}
	
	/*
	protected function GetUserByGUID($GUID) {
		return $this->sql->Call("GetUserByGUID", $GUID);
	}
	
	protected function GetUserByEmail($email) {
		return $this->sql->Call("GetUserByEmail", $email);
	}
	
	public static function Create($email, $firstName, $lastName, $password) {
		//create user..
		//$user->send_verification_email($id);
	}
	
	public function SendVerificationEmail($GUID) {
		
	}
	
	public function GetFollowers() {
		//call stored proc to get followers...
		$users = array();
		return $users;
	}
	
	public function GetPads() {
		//call stored proc to get all pads this user follows
		$pads = array();
		return $pads;
	}
	
	//Get 20 most recent posts.
	//If date provided, get 20 most recent as of date,
	//in this way all posts can be retrieved, by repeated calls using
	//the oldest date of the last post in the returned group 
	public function GetPadPosts($date = false) {
		if (!$date) { $date = date(); }
		$posts = array(); 
		return $posts;
	}*/
}
?>